#!/usr/bin/python
# -*- coding: utf-8 -*-
# database tool
import dbOperate
import collections
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
# file_dict = {}
file_dict = collections.OrderedDict()
db1 = dbOperate.dbOperate('10.37.133.21', 'spider', 'spider', 'spider')


# file => dict
def file_read():
    sql = 'select * from duty order by weekday'  # sql语句
    see = db1.exe_query(sql)
    file_dict = collections.OrderedDict()
    ####遍历see
    for data in see:
        if data:
            file_dict[str(data[0])] = data[1]
            # file_dict[data[0]] = data[1]

    # jsonData['hostname'] = hostname
    # jsonData['value'] = value
    # ##jsonData1 = sorted(jsonData.items(), key=lambda x: x[1])
    # ##sorted(jsonData.items(), key=lambda x: x[1])
    # j = json.dumps(jsonData, ensure_ascii=False)
    # print('person--------' + str(file_dict))
    return file_dict
    # with open('user.txt') as f:
    #     for line in f.read().split('\n'):
    #         if line:
    #             tmp = line.split(':')
    #             file_dict[tmp[0]] = tmp[1]
    #
    # return file_dict


# ditc => file
def file_write():
    # file_arr = []

    for user, pwd in file_dict.items():
        # file_arr.append('%s:%s' % (user, pwd))
        sql = 'update duty set person = ' + '"' + pwd + '"' + ' where weekday = ' + '"' + user + '"'
        # print(sql)
        db1.exe_query(sql)


# delete方法
def file_del(para):
    # file_arr = []
    sql = 'delete FROM duty where weekday =  ' + '"' + para + '"'
    # print(sql)
    db1.exe_query(sql)


def file_ins(para1, para2):
    sql = 'INSERT INTO duty (`weekday`, `person`) VALUES (' + '"' + para1 + '"' + ',' + '"' + para2 + '"' + ')'
    # print(sql)
    db1.exe_query(sql)


if __name__ == "__main__":
    # print(file_read())
    file_write()
